package com.survey.demo.ui.activity;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.android.material.tabs.TabLayout;
import com.hjq.widget.layout.WrapRecyclerView;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.survey.demo.R;
import com.survey.demo.app.AppActivity;
import com.survey.demo.app.AppAdapter;
import com.survey.demo.http.bean.SendBean;
import com.survey.demo.http.glide.GlideApp;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 我的界面
 */
public final class MySendActivity extends AppActivity implements OnRefreshLoadMoreListener {

    private TabLayout mTabLayout;
    private TextView tabSelect;
    private TextView tabUnSelect;
    private List<String> tabList=new ArrayList<>();
    private List<String> tabId=new ArrayList<>();
    private SmartRefreshLayout mRefreshLayout;
    private WrapRecyclerView mRecyclerView;
    private SendAdapter mAdapter;
    private List<SendBean> beans = new ArrayList<SendBean>();
    @Override
    protected int getLayoutId() {
        return R.layout.mysend_activity;
    }

    @Override
    protected void initView() {
        mTabLayout = findViewById(R.id.tab);
        tabList.clear();
        tabId.clear();
        tabList.add("企业招聘"); tabList.add("资质代办"); tabList.add("校企合作"); tabList.add("技能培训"); tabList.add("企业咨询"); tabList.add("项目对接"); tabList.add("软件销售");tabList.add("仪器配件");
        tabId.add("0");tabId.add("1");tabId.add("2");tabId.add("3");tabId.add("4");tabId.add("5");tabId.add("6");tabId.add("7");
//        for (int i = 0; i < leiMuBean.data.size(); i++) {
//            tabList.add(leiMuBean.data.get(i).classname);
//            tabId.add(leiMuBean.data.get(i).id + "");
//        }
        //添加标题
        for (int i = 0; i < tabList.size(); i++) {
            if(i==0){
                mTabLayout.addTab(mTabLayout.newTab().setText(tabList.get(i)),i,true);
            }else {
                mTabLayout.addTab(mTabLayout.newTab().setText(tabList.get(i)),i,false);
            }
        }
        mTabLayout.setTabRippleColor(ColorStateList.valueOf(Color.parseColor("#00000000")));
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
            updateTabTextView(mTabLayout.getTabAt(i), false);  //初始化每一个
        }
        //初始化选中的
        updateTabTextView(mTabLayout.getTabAt(mTabLayout.getSelectedTabPosition()), true);
        //底部导航栏切换监听
        mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                for(int i = 0; i<mTabLayout.getTabCount();i++){
                    updateTabTextView(mTabLayout.getTabAt(i), false);
                }
                updateTabTextView(tab, true);
                int position = tab.getPosition();

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                for(int i = 0; i<mTabLayout.getTabCount();i++){
                    updateTabTextView(mTabLayout.getTabAt(i), false);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        mRefreshLayout = findViewById(R.id.rl_status_refresh);
        mRecyclerView = findViewById(R.id.car_recycle);
        mAdapter = new SendAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
        mRefreshLayout.setOnRefreshLoadMoreListener(this);
    }

    private View getTabView(int currentPosition) {
        View view = LayoutInflater.from(this).inflate(R.layout.tab_item, null);
        TextView textView = (TextView) view.findViewById(R.id.tab_item_textview);
        textView.setText(tabList.get(currentPosition));
        return view;
    }

    private void updateTabTextView(TabLayout.Tab tab, boolean isSelect) {
        if (isSelect) {
            //选中加粗
            tabSelect = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
            tabSelect.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            tabSelect.setText(tab.getText());
            tabSelect.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        } else {
            tabUnSelect = (TextView) tab.getCustomView().findViewById(R.id.tab_item_textview);
            tabUnSelect.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            tabUnSelect.setText(tab.getText());
            tabUnSelect.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
        }
    }

    @Override
    protected void initData() {
         for(int i= 0;i<5;i++){
             SendBean sendBean = new SendBean();
             sendBean.setName("测量内业组长");
             sendBean.setCity("沈阳");
             sendBean.setDate("2021/06/30");
             beans.add(sendBean);
         }
        mAdapter.setData(beans);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        postDelayed(() -> {
//            mAdapter.clearData();
//            mAdapter.setData(analogData());
            mRefreshLayout.finishRefresh();
        }, 1000);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        postDelayed(() -> {
            //mAdapter.addData(analogData());
            mRefreshLayout.finishLoadMore();

            //mAdapter.setLastPage(mAdapter.getItemCount() >= 100);
            //mRefreshLayout.setNoMoreData(mAdapter.isLastPage());
        }, 1000);
    }

    class SendAdapter extends AppAdapter<SendBean> {

        public SendAdapter(Context context) {
            super(context);
        }

        @NonNull
        @Override
        public SendAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new SendAdapter.ViewHolder();
        }

        private final class ViewHolder extends AppAdapter<SendBean>.ViewHolder {

            private final CheckBox shop_logo;
            private final TextView shopName;
            private final TextView foodRecy;

            private ViewHolder() {
                super(R.layout.item_mysend);
                shop_logo = findViewById(R.id.item_check);
                shopName = findViewById(R.id.tv_name);
                foodRecy = findViewById(R.id.tv_city);
            }

            @Override
            public void onBindView(int position) {
                shopName.setText(getItem(position).getName());
                foodRecy.setText("发布城市："+getItem(position).getCity()+" | "+getItem(position).getDate());
            }
        }
    }
}